package com.galaxy.product.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.galaxy.common.core.utils.MapstructUtils;
import com.galaxy.common.core.utils.StringUtils;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.product.domain.ProductCategory;
import com.galaxy.product.domain.bo.ProductCategoryBo;
import com.galaxy.product.domain.vo.ProductCategoryVo;
import com.galaxy.product.mapper.ProductCategoryMapper;
import com.galaxy.product.service.IProductCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品分类Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class ProductCategoryServiceImpl implements IProductCategoryService {

    private final ProductCategoryMapper baseMapper;

    @DS("slave")
    @Override
    public TableDataInfo<ProductCategoryVo> queryPageList(ProductCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductCategory> lqw = buildQueryWrapper(bo);
        Page<ProductCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @DS("slave")
    @Override
    public List<ProductCategoryVo> queryList(ProductCategoryBo bo) {
        LambdaQueryWrapper<ProductCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @DS("slave")
    @Override
    public List<ProductCategoryVo> queryTree() {
        List<ProductCategoryVo> categoryList = baseMapper.selectVoList(new LambdaQueryWrapper<ProductCategory>()
            .orderByAsc(ProductCategory::getSort));

        return buildTree(categoryList);
    }

    @DS("slave")
    @Override
    public ProductCategory getInfo(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ProductCategoryBo bo) {
        ProductCategory add = MapstructUtils.convert(bo, ProductCategory.class);
        baseMapper.insert(add);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProductCategoryBo bo) {
        ProductCategory update = MapstructUtils.convert(bo, ProductCategory.class);
        baseMapper.updateById(update);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            baseMapper.deleteById(id);
        }
    }

    private LambdaQueryWrapper<ProductCategory> buildQueryWrapper(ProductCategoryBo bo) {
        LambdaQueryWrapper<ProductCategory> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ProductCategory::getName, bo.getName());
        lqw.eq(bo.getParentId() != null, ProductCategory::getParentId, bo.getParentId());
        lqw.eq(bo.getLevel() != null, ProductCategory::getLevel, bo.getLevel());
        lqw.eq(bo.getStatus() != null, ProductCategory::getStatus, bo.getStatus());
        lqw.orderByAsc(ProductCategory::getSort);
        return lqw;
    }

    private List<ProductCategoryVo> buildTree(List<ProductCategoryVo> categoryList) {
        Map<Long, List<ProductCategoryVo>> childrenMap = categoryList.stream()
            .filter(category -> category.getParentId() != null && category.getParentId() != 0L)
            .collect(Collectors.groupingBy(ProductCategoryVo::getParentId));

        categoryList.forEach(category -> category.setChildren(childrenMap.get(category.getId())));

        return categoryList.stream()
            .filter(category -> category.getParentId() == null || category.getParentId() == 0L)
            .collect(Collectors.toList());
    }
}
